package com.taiji.dicom4cloud.common.core.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author fxl
 * @since 2019/04/28
 */
public class DateUtil {
    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取当前时间字符串
     */
    public static LocalDateTime localDateTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime.atZone(ZoneId.of(ZoneId.SHORT_IDS.get("CTT"))).toInstant();
        return localDateTime;
    }

    public static LocalDate getLocalDate(String date) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static LocalDate getLocalDateTime(String date) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String getLocalDateToString() {
        Date date = new Date();
        return format.format(date);
    }

    /**
     * 字符串转化LocalDateTime
     *
     * @param localDateTime
     * @return
     */
    public static LocalDateTime StringToLocalDateTime(String localDateTime) {
        if (StrUtil.isEmpty(localDateTime)) {
            return null;
        }
        return LocalDateTime.parse(localDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String localDateTimeToString(LocalDateTime localDateTime) {
        if (ObjectUtil.isNull(localDateTime)) {
            return "";
        }
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }



    /**
     * 时间转化
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * 根据传入超时时间获取超时计算开始时间
     */
    public static String getTimeOutStartTime(Integer timeOut) {
        Calendar c = Calendar.getInstance();
        Calendar ct = Calendar.getInstance();
        Date dateTime;
        c.setTime(new Date());
        //  c.add(Calendar.HOUR_OF_DAY, -24);
        ct.setTime(c.getTime());
        ct.add(Calendar.MINUTE, -timeOut);
        dateTime = ct.getTime();
        Instant instant = dateTime.toInstant();
        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 根据年龄获取出生日期
     *
     * @return
     */
    public static LocalDate getBirthDayByAge(Integer timeType, Integer age) {
        Calendar c = Calendar.getInstance();
        Calendar ct = Calendar.getInstance();
        Date dateTime;
        c.setTime(new Date());
        ct.setTime(c.getTime());
        switch (timeType) {
            case 0:
                ct.add(Calendar.YEAR, -age);
                break;
            case 1:
                ct.add(Calendar.MONTH, -age);
                break;
            case 2:
                ct.add(Calendar.DATE, -age);
                break;
        }

        dateTime = ct.getTime();
        Instant instant = dateTime.toInstant();
        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
        return instant.atZone(zoneId).toLocalDate();
    }

    /**
     * 获取计划时间
     */
    public static LocalDateTime getPlanTime(LocalDateTime regeteTime, Integer timeOut) {
        Calendar c = Calendar.getInstance();
        Calendar ct = Calendar.getInstance();
        Date dateTime;
        c.setTime(localDateTime2Date(regeteTime));
        //  c.add(Calendar.HOUR_OF_DAY, -24);
        ct.setTime(c.getTime());
        ct.add(Calendar.MINUTE, timeOut);
        dateTime = ct.getTime();
        Instant instant = dateTime.toInstant();
        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 获取报告固化时间
     *
     * @param solidTime
     * @return
     */
    public static LocalDateTime getReportSolidTime(Integer solidTime) {
        Calendar ct = Calendar.getInstance();
        Date dateTime;
        ct.setTime(new Date());
        ct.add(Calendar.MINUTE, -solidTime);
        dateTime = ct.getTime();
        Instant instant = dateTime.toInstant();
        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    public static String getTimeOutEndTime() {
        Calendar c = Calendar.getInstance();
        Date dateTime;
        c.setTime(new Date());
        c.add(Calendar.HOUR_OF_DAY, -24);
        dateTime = c.getTime();
        Instant instant = dateTime.toInstant();
        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 取得时间差
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static Integer getDateDiffTime(LocalDateTime startTime, LocalDateTime endTime) {

        Long time = Duration.between(startTime, endTime).toMinutes();
        //不足一分钟按照一分钟计算
        if (time < 1) {
            time = 1L;
        }
        BigDecimal decimal = new BigDecimal(time);
        decimal.setScale(0, BigDecimal.ROUND_DOWN);
        return Integer.parseInt(decimal.toBigInteger().toString());
    }

    /**
     * 时间转化
     *
     * @param mss
     * @return
     */
    public static String formatDateTime(Integer mss) {
        if (mss == null) {
            return null;
        }
        String DateTimes = null;
        long days = mss / (60 * 24);
        long hours = (mss % (60 * 24)) / (60);
        long minutes = (mss % (60));
        long seconds = mss;
        if (days > 0) {
            DateTimes = days + "天" + hours + "小时" + minutes + "分";
        } else if (hours > 0) {
            DateTimes = hours + "小时" + minutes + "分";
        } else if (minutes > 0) {
            DateTimes = minutes + "分";
        } else {
            DateTimes = seconds + "秒";
        }

        return DateTimes;
    }

    /**
     * 时间转化
     *
     * @param mss
     * @return
     */
    public static String formatDateTime2(Integer mss) {
        if (mss == null) {
            return null;
        }
        String DateTimes = null;
        long days = mss / (60 * 24);
        long hours = (mss % (60 * 24)) / (60);
        long minutes = (mss % (60));
        long seconds = mss;
        if (days > 0) {
            DateTimes = days + "天" + hours + "小时" + minutes + "分钟";
        } else if (hours > 0) {
            DateTimes = hours + "小时" + minutes + "分钟";
        } else if (minutes > 0) {
            DateTimes = minutes + "分钟";
        } else {
            DateTimes = seconds + "秒";
        }

        return DateTimes;
    }

    public static void main(String[] args) {
        Integer dd = 90;
        String ss = formatDateTime(dd);
        System.out.println(ss);
    }

    public static String getDateDir() {
        Date date = new Date();
        String currentDate = DateFormatUtils.format(date, "yyyyMMdd");
        return currentDate;
    }

    public static String getDatePreDayDir(Integer preDay) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, preDay);
        String currentDate = DateFormatUtils.format(c.getTime(), "yyyyMMdd");
        return currentDate;
    }

}
